package com.notebad.ui;

import java.io.File;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

import com.notebad.R;
import com.notebad.note.Note;

/**
 * Liste des sons associes a une note
 */
public class SoundListView extends ListActivity {

	@SuppressWarnings("unused")
	private static final String TAG = "[SoundsListView]";
	private static final int AUDIO_ID = 0;

	private LinearLayout bgLayout;
	private Drawable bgDrawable;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sound_list);

		bgLayout = (LinearLayout)findViewById(R.id.soundListBG);
		bgDrawable = this.getResources().getDrawable(R.drawable.ic_bg_welcome);
		bgLayout.setBackgroundDrawable(bgDrawable);
		
		getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int index, long arg3) {
				AlertDialog.Builder builder = new AlertDialog.Builder(SoundListView.this);
				builder.setMessage("Supprimer cet enregistrement ?")
				       .setCancelable(false)
				       .setPositiveButton("Oui", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				        	   //Supprimer ici le fichier correspondant a cet enregistrement
				        	   String[] sounds = Note.getCurrentNote().getSounds();
								File tmp = new File(sounds[index]);
								if(tmp.exists()){
									tmp.delete();
								}
								onResume();
				           }
				       })
				       .setNegativeButton("Non", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
				return false;
			}
		});
	}


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		//Lancer une Activity qui fasse player
		String[] sounds = Note.getCurrentNote().getSounds();
		File tmp = new File(sounds[position]);
		if(tmp.exists())
		{
			Intent player = new Intent(SoundListView.this,AudioPlayerView.class);
			player.putExtra("soundPath", sounds[position]);
			startActivity(player);
		}
	}
	
	
	
	@Override
	protected void onResume() {
		super.onResume();
		fillList();
	}
	
	private void fillList()
	{
		String[] soundsPath = Note.getCurrentNote().getSounds();
		String[] soundsName = new String[soundsPath.length];
		for (int i = 0; i < soundsPath.length; i++) {
			int lastSlash = soundsPath[i].lastIndexOf('/');
			soundsName[i] = soundsPath[i].substring(lastSlash+1);
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, soundsName);
		setListAdapter(adapter);
		if(Note.getCurrentNote().getSounds().length == 0){
			Toast.makeText(getBaseContext(), "Aucun enregistrement pour cette note", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, AUDIO_ID, 0, R.string.SoundsListView_menu_addSound).setIcon(R.drawable.ic_menu_micro);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		case AUDIO_ID:
			Intent recorder = new Intent(this,AudioRecorderView.class);
			startActivity(recorder);
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}
}
